package com.leon.notepad.content;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.TieBean;
import com.dou361.dialogui.listener.DialogUIItemListener;
import com.leon.notepad.R;
import com.leon.notepad.calendarprovider.calendar.CalendarEvent;
import com.leon.notepad.calendarprovider.calendar.CalendarProviderManager;
import com.leon.notepad.content.adapter.MyRemindRecyclerAdapter;
import com.leon.notepad.content.utils.WrapContentLinearLayoutManager;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RemindActivity extends BaseActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private List<CalendarEvent> mList = new ArrayList<>();
    private MyRemindRecyclerAdapter adapter;

    private static final int REQUEST_SETTING_MONEY = 1015;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remind);
        ButterKnife.bind(this);

        // 查询所有事件
        long calID4 = CalendarProviderManager.obtainCalendarAccountID(this);
        List<CalendarEvent> events = CalendarProviderManager.queryAccountEvent(this, calID4);
        if (null != events) {
            mList.addAll(events);
            Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
        }

        //设置布局管理器
        recyclerview.setLayoutManager(new WrapContentLinearLayoutManager(this, 1));
        adapter = new MyRemindRecyclerAdapter(mList, this);
        recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new MyRemindRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }

            @Override
            public void onItemLongClick(View view, int position) {
                List<TieBean> datass = new ArrayList<>();
                datass.add(new TieBean("删除"));
                DialogUIUtils.showSheet(RemindActivity.this, datass,
                        "取消", Gravity.BOTTOM, true, true, new DialogUIItemListener() {
                            @Override
                            public void onItemClick(CharSequence text, int i) {
                                Log.e("1111", "!!!!!!!!" + position);
                                // 删除事件
                                long eventID = mList.get(position).getId();
                                int result2 = CalendarProviderManager.deleteCalendarEvent(RemindActivity.this, eventID);
                                if (result2 == -2) {
                                    Toast.makeText(RemindActivity.this, "没有权限", Toast.LENGTH_SHORT).show();
                                } else {
                                    mList.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(RemindActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                }

                            }
                        }).show();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.item_add:
                Intent intent = new Intent(RemindActivity.this, AddRemindActivity.class);
                startActivityForResult(intent,REQUEST_SETTING_MONEY);
                break;
        }
        return true;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SETTING_MONEY && resultCode == RESULT_OK) {
            // 查询所有事件
            long calID4 = CalendarProviderManager.obtainCalendarAccountID(this);
            List<CalendarEvent> events = CalendarProviderManager.queryAccountEvent(this, calID4);
            if (null != events) {
                mList.clear();
                mList.addAll(events);
                adapter.notifyDataSetChanged();
                Toast.makeText(this, "查询成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "查询失败", Toast.LENGTH_SHORT).show();
            }

        }
    }
}